home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.1 KB  |  795 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zchar.c,v 1.3 2000/09/19 19:00:52 lpd Exp $ */
  20. /* Character operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gsstruct.h"
  24. #include "gstext.h"
  25. #include "gxarith.h"
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"        /* for ifont.h */
  28. #include "gxdevice.h"        /* for gxfont.h */
  29. #include "gxfont.h"
  30. #include "gzstate.h"
  31. #include "dstack.h"        /* for stack depth */
  32. #include "estack.h"
  33. #include "ialloc.h"
  34. #include "ichar.h"
  35. #include "idict.h"
  36. #include "ifont.h"
  37. #include "igstate.h"
  38. #include "ilevel.h"
  39. #include "iname.h"
  40. #include "ipacked.h"
  41. #include "store.h"
  42.  
  43. /* Forward references */
  44. private bool map_glyph_to_char(P3(const ref *, const ref *, ref *));
  45. private int finish_show(P1(i_ctx_t *));
  46. private int op_show_cleanup(P1(i_ctx_t *));
  47. private int op_show_return_width(P3(i_ctx_t *, uint, double *));
  48.  
  49. /* <string> show - */
  50. private int
  51. zshow(i_ctx_t *i_ctx_p)
  52. {
  53.     os_ptr op = osp;
  54.     gs_text_enum_t *penum;
  55.     int code = op_show_setup(i_ctx_p, op);
  56.  
  57.     if (code != 0 ||
  58.     (code = gs_show_begin(igs, op->value.bytes, r_size(op), imemory, &penum)) < 0)
  59.     return code;
  60.     if ((code = op_show_finish_setup(i_ctx_p, penum, 1, finish_show)) < 0) {
  61.     ifree_object(penum, "op_show_enum_setup");
  62.     return code;
  63.     }
  64.     return op_show_continue_pop(i_ctx_p, 1);
  65. }
  66.  
  67. /* <ax> <ay> <string> ashow - */
  68. private int
  69. zashow(i_ctx_t *i_ctx_p)
  70. {
  71.     os_ptr op = osp;
  72.     gs_text_enum_t *penum;
  73.     double axy[2];
  74.     int code = num_params(op - 1, 2, axy);
  75.  
  76.     if (code < 0 ||
  77.     (code = op_show_setup(i_ctx_p, op)) != 0 ||
  78.     (code = gs_ashow_begin(igs, axy[0], axy[1], op->value.bytes, r_size(op), imemory, &penum)) < 0)
  79.     return code;
  80.     if ((code = op_show_finish_setup(i_ctx_p, penum, 3, finish_show)) < 0) {
  81.     ifree_object(penum, "op_show_enum_setup");
  82.     return code;
  83.     }
  84.     return op_show_continue_pop(i_ctx_p, 3);
  85. }
  86.  
  87. /* <cx> <cy> <char> <string> widthshow - */
  88. private int
  89. zwidthshow(i_ctx_t *i_ctx_p)
  90. {
  91.     os_ptr op = osp;
  92.     gs_text_enum_t *penum;
  93.     double cxy[2];
  94.     int code;
  95.  
  96.     check_type(op[-1], t_integer);
  97.     if ((gs_char) (op[-1].value.intval) != op[-1].value.intval)
  98.     return_error(e_rangecheck);
  99.     if ((code = num_params(op - 2, 2, cxy)) < 0 ||
  100.     (code = op_show_setup(i_ctx_p, op)) != 0 ||
  101.     (code = gs_widthshow_begin(igs, cxy[0], cxy[1],
  102.                    (gs_char) op[-1].value.intval,
  103.                    op->value.bytes, r_size(op),
  104.                    imemory, &penum)) < 0)
  105.     return code;
  106.     if ((code = op_show_finish_setup(i_ctx_p, penum, 4, finish_show)) < 0) {
  107.     ifree_object(penum, "op_show_enum_setup");
  108.     return code;
  109.     }
  110.     return op_show_continue_pop(i_ctx_p, 4);
  111. }
  112.  
  113. /* <cx> <cy> <char> <ax> <ay> <string> awidthshow - */
  114. private int
  115. zawidthshow(i_ctx_t *i_ctx_p)
  116. {
  117.     os_ptr op = osp;
  118.     gs_text_enum_t *penum;
  119.     double cxy[2], axy[2];
  120.     int code;
  121.  
  122.     check_type(op[-3], t_integer);
  123.     if ((gs_char) (op[-3].value.intval) != op[-3].value.intval)
  124.     return_error(e_rangecheck);
  125.     if ((code = num_params(op - 4, 2, cxy)) < 0 ||
  126.     (code = num_params(op - 1, 2, axy)) < 0 ||
  127.     (code = op_show_setup(i_ctx_p, op)) != 0 ||
  128.     (code = gs_awidthshow_begin(igs, cxy[0], cxy[1],
  129.                     (gs_char) op[-3].value.intval,
  130.                     axy[0], axy[1],
  131.                     op->value.bytes, r_size(op),
  132.                     imemory, &penum)) < 0)
  133.     return code;
  134.     if ((code = op_show_finish_setup(i_ctx_p, penum, 6, finish_show)) < 0) {
  135.     ifree_object(penum, "op_show_enum_setup");
  136.     return code;
  137.     }
  138.     return op_show_continue_pop(i_ctx_p, 6);
  139. }
  140.  
  141. /* <proc> <string> kshow - */
  142. private int
  143. zkshow(i_ctx_t *i_ctx_p)
  144. {
  145.     os_ptr op = osp;
  146.     gs_text_enum_t *penum;
  147.     int code;
  148.  
  149.     check_proc(op[-1]);
  150.     if ((code = op_show_setup(i_ctx_p, op)) != 0 ||
  151.     (code = gs_kshow_begin(igs, op->value.bytes, r_size(op),
  152.                    imemory, &penum)) < 0)
  153.     return code;
  154.     if ((code = op_show_finish_setup(i_ctx_p, penum, 2, finish_show)) < 0) {
  155.     ifree_object(penum, "op_show_enum_setup");
  156.     return code;
  157.     }
  158.     sslot = op[-1];        /* save kerning proc */
  159.     return op_show_continue_pop(i_ctx_p, 2);
  160. }
  161.  
  162. /* Common finish procedure for all show operations. */
  163. /* Doesn't have to do anything. */
  164. private int
  165. finish_show(i_ctx_t *i_ctx_p)
  166. {
  167.     return 0;
  168. }
  169.  
  170. /* <string> stringwidth <wx> <wy> */
  171. private int
  172. zstringwidth(i_ctx_t *i_ctx_p)
  173. {
  174.     os_ptr op = osp;
  175.     gs_text_enum_t *penum;
  176.     int code = op_show_setup(i_ctx_p, op);
  177.  
  178.     if (code != 0 ||
  179.     (code = gs_stringwidth_begin(igs, op->value.bytes, r_size(op),
  180.                      imemory, &penum)) < 0)
  181.     return code;
  182.     if ((code = op_show_finish_setup(i_ctx_p, penum, 1, finish_stringwidth)) < 0) {
  183.     ifree_object(penum, "op_show_enum_setup");
  184.     return code;
  185.     }
  186.     return op_show_continue_pop(i_ctx_p, 1);
  187. }
  188. /* Finishing procedure for stringwidth. */
  189. /* Pushes the accumulated width. */
  190. /* This is exported for .glyphwidth (in zcharx.c). */
  191. int
  192. finish_stringwidth(i_ctx_t *i_ctx_p)
  193. {
  194.     os_ptr op = osp;
  195.     gs_point width;
  196.  
  197.     gs_text_total_width(senum, &width);
  198.     push(2);
  199.     make_real(op - 1, width.x);
  200.     make_real(op, width.y);
  201.     return 0;
  202. }
  203.  
  204. /* Common code for charpath and .charboxpath. */
  205. private int
  206. zchar_path(i_ctx_t *i_ctx_p,
  207.        int (*begin)(P6(gs_state *, const byte *, uint,
  208.                bool, gs_memory_t *, gs_text_enum_t **)))
  209. {
  210.     os_ptr op = osp;
  211.     gs_text_enum_t *penum;
  212.     int code;
  213.  
  214.     check_type(*op, t_boolean);
  215.     code = op_show_setup(i_ctx_p, op - 1);
  216.     if (code != 0 ||
  217.     (code = begin(igs, op[-1].value.bytes, r_size(op - 1),
  218.               op->value.boolval, imemory, &penum)) < 0)
  219.     return code;
  220.     if ((code = op_show_finish_setup(i_ctx_p, penum, 2, finish_show)) < 0) {
  221.     ifree_object(penum, "op_show_enum_setup");
  222.     return code;
  223.     }
  224.     return op_show_continue_pop(i_ctx_p, 2);
  225. }
  226. /* <string> <outline_bool> charpath - */
  227. private int
  228. zcharpath(i_ctx_t *i_ctx_p)
  229. {
  230.     return zchar_path(i_ctx_p, gs_charpath_begin);
  231. }
  232. /* <string> <box_bool> .charboxpath - */
  233. private int
  234. zcharboxpath(i_ctx_t *i_ctx_p)
  235. {
  236.     return zchar_path(i_ctx_p, gs_charboxpath_begin);
  237. }
  238.  
  239. /* <wx> <wy> <llx> <lly> <urx> <ury> setcachedevice - */
  240. int
  241. zsetcachedevice(i_ctx_t *i_ctx_p)
  242. {
  243.     os_ptr op = osp;
  244.     double wbox[6];
  245.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  246.     int code = num_params(op, 6, wbox);
  247.  
  248.     if (penum == 0)
  249.     return_error(e_undefined);
  250.     if (code < 0)
  251.     return code;
  252.     if (zchar_show_width_only(penum))
  253.     return op_show_return_width(i_ctx_p, 6, &wbox[0]);
  254.     code = gs_text_setcachedevice(penum, wbox);
  255.     if (code < 0)
  256.     return code;
  257.     pop(6);
  258.     if (code == 1)
  259.     clear_pagedevice(istate);
  260.     return 0;
  261. }
  262.  
  263. /* <w0x> <w0y> <llx> <lly> <urx> <ury> <w1x> <w1y> <vx> <vy> setcachedevice2 - */
  264. int
  265. zsetcachedevice2(i_ctx_t *i_ctx_p)
  266. {
  267.     os_ptr op = osp;
  268.     double wbox[10];
  269.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  270.     int code = num_params(op, 10, wbox);
  271.  
  272.     if (penum == 0)
  273.     return_error(e_undefined);
  274.     if (code < 0)
  275.     return code;
  276.     if (zchar_show_width_only(penum))
  277.     return op_show_return_width(i_ctx_p, 10,
  278.                     (gs_rootfont(igs)->WMode ?
  279.                      &wbox[6] : &wbox[0]));
  280.     code = gs_text_setcachedevice2(penum, wbox);
  281.     if (code < 0)
  282.     return code;
  283.     pop(10);
  284.     if (code == 1)
  285.     clear_pagedevice(istate);
  286.     return 0;
  287. }
  288.  
  289. /* <wx> <wy> setcharwidth - */
  290. private int
  291. zsetcharwidth(i_ctx_t *i_ctx_p)
  292. {
  293.     os_ptr op = osp;
  294.     double width[2];
  295.     gs_text_enum_t *penum = op_show_find(i_ctx_p);
  296.     int code = num_params(op, 2, width);
  297.  
  298.     if (penum == 0)
  299.     return_error(e_undefined);
  300.     if (code < 0)
  301.     return code;
  302.     if (zchar_show_width_only(penum))
  303.     return op_show_return_width(i_ctx_p, 2, &width[0]);
  304.     code = gs_text_setcharwidth(penum, width);
  305.     if (code < 0)
  306.     return code;
  307.     pop(2);
  308.     return 0;
  309. }
  310.  
  311. /* <dict> .fontbbox <llx> <lly> <urx> <ury> -true- */
  312. /* <dict> .fontbbox -false- */
  313. private int
  314. zfontbbox(i_ctx_t *i_ctx_p)
  315. {
  316.     os_ptr op = osp;
  317.     double bbox[4];
  318.     int code;
  319.  
  320.     check_type(*op, t_dictionary);
  321.     check_dict_read(*op);
  322.     code = font_bbox_param(op, bbox);
  323.     if (code < 0)
  324.     return code;
  325.     if (bbox[0] < bbox[2] && bbox[1] < bbox[3]) {
  326.     push(4);
  327.     make_reals(op - 4, bbox, 4);
  328.     make_true(op);
  329.     } else {            /* No bbox, or an empty one. */
  330.     make_false(op);
  331.     }
  332.     return 0;
  333. }
  334.  
  335. /* ------ Initialization procedure ------ */
  336.  
  337. const op_def zchar_op_defs[] =
  338. {
  339.     {"3ashow", zashow},
  340.     {"6awidthshow", zawidthshow},
  341.     {"2charpath", zcharpath},
  342.     {"2.charboxpath", zcharboxpath},
  343.     {"2kshow", zkshow},
  344.     {"6setcachedevice", zsetcachedevice},
  345.     {":setcachedevice2", zsetcachedevice2},
  346.     {"2setcharwidth", zsetcharwidth},
  347.     {"1show", zshow},
  348.     {"1stringwidth", zstringwidth},
  349.     {"4widthshow", zwidthshow},
  350.         /* Extensions */
  351.     {"1.fontbbox", zfontbbox},
  352.         /* Internal operators */
  353.     {"0%finish_show", finish_show},
  354.     {"0%finish_stringwidth", finish_stringwidth},
  355.     {"0%op_show_continue", op_show_continue},
  356.     op_def_end(0)
  357. };
  358.  
  359. /* ------ Subroutines ------ */
  360.  
  361. /* Most of these are exported for zchar2.c. */
  362.  
  363. /* Convert a glyph to a ref. */
  364. void
  365. glyph_ref(gs_glyph glyph, ref * gref)
  366. {
  367.     if (glyph < gs_min_cid_glyph)
  368.     name_index_ref(glyph, gref);
  369.     else
  370.     make_int(gref, glyph - gs_min_cid_glyph);
  371. }
  372.  
  373. /* Prepare to set up for a text operator. */
  374. /* Don't change any state yet. */
  375. int
  376. op_show_setup(i_ctx_t *i_ctx_p, os_ptr op)
  377. {
  378.     check_read_type(*op, t_string);
  379.     return op_show_enum_setup(i_ctx_p);
  380. }
  381. int
  382. op_show_enum_setup(i_ctx_t *i_ctx_p)
  383. {
  384.     check_estack(snumpush + 2);
  385.     return 0;
  386. }
  387.  
  388. /* Finish setting up a text operator. */
  389. int
  390. op_show_finish_setup(i_ctx_t *i_ctx_p, gs_text_enum_t * penum, int npop,
  391.              op_proc_t endproc /* end procedure */ )
  392. {
  393.     gs_text_enum_t *osenum = op_show_find(i_ctx_p);
  394.     es_ptr ep = esp + snumpush;
  395.     gs_glyph glyph;
  396.  
  397.     /*
  398.      * If we are in the procedure of a cshow for a CID font and this is
  399.      * a show operator, do something special, per the Red Book.
  400.      */
  401.     if (osenum &&
  402.     SHOW_IS_ALL_OF(osenum,
  403.                TEXT_FROM_STRING | TEXT_DO_NONE | TEXT_INTERVENE) &&
  404.     SHOW_IS_ALL_OF(penum, TEXT_FROM_STRING | TEXT_RETURN_WIDTH) &&
  405.     (glyph = gs_text_current_glyph(osenum)) != gs_no_glyph &&
  406.     glyph >= gs_min_cid_glyph
  407.     ) {
  408.     gs_text_params_t text;
  409.  
  410.     if (!(penum->text.size == 1 &&
  411.           penum->text.data.bytes[0] ==
  412.             (gs_text_current_char(osenum) & 0xff))
  413.         )
  414.         return_error(e_rangecheck);
  415.     text = penum->text;
  416.     text.operation =
  417.         (text.operation &
  418.          ~(TEXT_FROM_STRING | TEXT_FROM_BYTES | TEXT_FROM_CHARS |
  419.            TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_CHAR)) |
  420.         TEXT_FROM_SINGLE_GLYPH;
  421.     text.data.d_glyph = glyph;
  422.     text.size = 1;
  423.     gs_text_restart(penum, &text);
  424.     }
  425.     make_mark_estack(ep - (snumpush - 1), es_show, op_show_cleanup);
  426.     if (endproc == NULL)
  427.     endproc = finish_show;
  428.     make_null(&esslot(ep));
  429.     make_int(&esodepth(ep), 0);    /* see gs_show_render case in */
  430.     /* op_show_continue_dispatch */
  431.     make_int(&esddepth(ep), 0);    /* ditto */
  432.     make_int(&esgslevel(ep), igs->level);
  433.     make_null(&essfont(ep));
  434.     make_null(&esrfont(ep));
  435.     make_op_estack(&eseproc(ep), endproc);
  436.     make_istruct(ep, 0, penum);
  437.     esp = ep;
  438.     return 0;
  439. }
  440.  
  441. /* Continuation operator for character rendering. */
  442. int
  443. op_show_continue(i_ctx_t *i_ctx_p)
  444. {
  445.     return op_show_continue_dispatch(i_ctx_p, 0, gs_text_process(senum));
  446. }
  447. int
  448. op_show_continue_pop(i_ctx_t *i_ctx_p, int npop)
  449. {
  450.     return op_show_continue_dispatch(i_ctx_p, npop, gs_text_process(senum));
  451. }
  452. /*
  453.  * Note that op_show_continue_dispatch sets osp = op explicitly iff the
  454.  * dispatch succeeds.  This is so that the show operators don't pop anything
  455.  * from the o-stack if they don't succeed.  Note also that if it returns an
  456.  * error, it has freed the enumerator.
  457.  */
  458. int
  459. op_show_continue_dispatch(i_ctx_t *i_ctx_p, int npop, int code)
  460. {
  461.     os_ptr op = osp - npop;
  462.     gs_text_enum_t *penum = senum;
  463.  
  464.     switch (code) {
  465.     case 0: {        /* all done */
  466.         os_ptr save_osp = osp;
  467.  
  468.         osp = op;
  469.         code = (*real_opproc(&seproc)) (i_ctx_p);
  470.         op_show_free(i_ctx_p, code);
  471.         if (code < 0) {
  472.         osp = save_osp;
  473.         return code;
  474.         }
  475.         return o_pop_estack;
  476.     }
  477.     case TEXT_PROCESS_INTERVENE: {
  478.         ref *pslot = &sslot; /* only used for kshow */
  479.  
  480.         push(2);
  481.         make_int(op - 1, gs_text_current_char(penum)); /* previous char */
  482.         make_int(op, gs_text_next_char(penum));
  483.         push_op_estack(op_show_continue);    /* continue after kerning */
  484.         *++esp = *pslot;    /* kerning procedure */
  485.         return o_push_estack;
  486.     }
  487.     case TEXT_PROCESS_RENDER: {
  488.         gs_font *pfont = gs_currentfont(igs);
  489.         font_data *pfdata = pfont_data(pfont);
  490.         gs_char chr = gs_text_current_char(penum);
  491.         gs_glyph glyph = gs_text_current_glyph(penum);
  492.  
  493.         push(2);
  494.         op[-1] = pfdata->dict;    /* push the font */
  495.         /*
  496.          * For Type 1 and Type 4 fonts, prefer BuildChar to BuildGlyph
  497.          * if there is no glyph, or if there is both a character and a
  498.          * glyph and the glyph is the one that corresponds to the
  499.          * character in the Encoding, so that PostScript procedures
  500.          * appearing in the CharStrings dictionary will receive the
  501.          * character code rather than the character name; for Type 3
  502.          * fonts, prefer BuildGlyph to BuildChar.  For other font types
  503.          * (such as CID fonts), only BuildGlyph will be present.
  504.          */
  505.         if (pfont->FontType == ft_user_defined) {
  506.         /* Type 3 font, prefer BuildGlyph. */
  507.         if (level2_enabled &&
  508.             !r_has_type(&pfdata->BuildGlyph, t_null) &&
  509.             glyph != gs_no_glyph
  510.             ) {
  511.             glyph_ref(glyph, op);
  512.             esp[2] = pfdata->BuildGlyph;
  513.         } else if (r_has_type(&pfdata->BuildChar, t_null))
  514.             goto err;
  515.         else if (chr == gs_no_char) {
  516.             /* glyphshow, reverse map the character */
  517.             /* through the Encoding */
  518.             ref gref;
  519.             const ref *pencoding = &pfdata->Encoding;
  520.  
  521.             glyph_ref(glyph, &gref);
  522.             if (!map_glyph_to_char(&gref, pencoding,
  523.                        (ref *) op)
  524.             ) {    /* Not found, try .notdef */
  525.             name_enter_string(".notdef", &gref);
  526.             if (!map_glyph_to_char(&gref,
  527.                            pencoding,
  528.                            (ref *) op)
  529.                 )
  530.                 goto err;
  531.             }
  532.             esp[2] = pfdata->BuildChar;
  533.         } else {
  534.             make_int(op, chr & 0xff);
  535.             esp[2] = pfdata->BuildChar;
  536.         }
  537.         } else {
  538.         /*
  539.          * For a Type 1 or Type 4 font, prefer BuildChar or
  540.          * BuildGlyph as described above: we know that both
  541.          * BuildChar and BuildGlyph are present.  For other font
  542.          * types, only BuildGlyph is available.
  543.          */
  544.         ref eref, gref;
  545.  
  546.         if (chr != gs_no_char &&
  547.             !r_has_type(&pfdata->BuildChar, t_null) &&
  548.             (glyph == gs_no_glyph ||
  549.              (array_get(&pfdata->Encoding, (long)(chr & 0xff), &eref) >= 0 &&
  550.               (glyph_ref(glyph, &gref), obj_eq(&gref, &eref))))
  551.             ) {
  552.             make_int(op, chr & 0xff);
  553.             esp[2] = pfdata->BuildChar;
  554.         } else {
  555.             /* We might not have a glyph: substitute 0. **HACK** */
  556.             if (glyph == gs_no_glyph)
  557.             make_int(op, 0);
  558.             else
  559.             glyph_ref(glyph, op);
  560.             esp[2] = pfdata->BuildGlyph;
  561.         }
  562.         }
  563.         /* Save the stack depths in case we bail out. */
  564.         sodepth.value.intval = ref_stack_count(&o_stack) - 2;
  565.         sddepth.value.intval = ref_stack_count(&d_stack);
  566.         push_op_estack(op_show_continue);
  567.         ++esp;        /* skip BuildChar or BuildGlyph proc */
  568.         return o_push_estack;
  569.     }
  570.     default:        /* error */
  571. err:
  572.         if (code >= 0)
  573.         code = gs_note_error(e_invalidfont);
  574.         return op_show_free(i_ctx_p, code);
  575.     }
  576. }
  577. /* Reverse-map a glyph name to a character code for glyphshow. */
  578. private bool
  579. map_glyph_to_char(const ref * pgref, const ref * pencoding, ref * pch)
  580. {
  581.     uint esize = r_size(pencoding);
  582.     uint ch;
  583.     ref eref;
  584.  
  585.     for (ch = 0; ch < esize; ch++) {
  586.     array_get(pencoding, (long)ch, &eref);
  587.     if (obj_eq(pgref, &eref)) {
  588.         make_int(pch, ch);
  589.         return true;
  590.     }
  591.     }
  592.     return false;
  593. }
  594.  
  595. /* Find the index of the e-stack mark for the current show enumerator. */
  596. /* Return 0 if we can't find the mark. */
  597. private uint
  598. op_show_find_index(i_ctx_t *i_ctx_p)
  599. {
  600.     ref_stack_enum_t rsenum;
  601.     uint count = 0;
  602.  
  603.     ref_stack_enum_begin(&rsenum, &e_stack);
  604.     do {
  605.     es_ptr ep = rsenum.ptr;
  606.     uint size = rsenum.size;
  607.  
  608.     for (ep += size - 1; size != 0; size--, ep--, count++)
  609.         if (r_is_estack_mark(ep) && estack_mark_index(ep) == es_show)
  610.         return count;
  611.     } while (ref_stack_enum_next(&rsenum));
  612.     return 0;        /* no mark */
  613. }
  614.  
  615. /* Find the current show enumerator on the e-stack. */
  616. gs_text_enum_t *
  617. op_show_find(i_ctx_t *i_ctx_p)
  618. {
  619.     uint index = op_show_find_index(i_ctx_p);
  620.  
  621.     if (index == 0)
  622.     return 0;        /* no mark */
  623.     return r_ptr(ref_stack_index(&e_stack, index - (snumpush - 1)),
  624.          gs_text_enum_t);
  625. }
  626.  
  627. /*
  628.  * Return true if we only need the width from the rasterizer
  629.  * and can short-circuit the full rendering of the character,
  630.  * false if we need the actual character bits.  This is only safe if
  631.  * we know the character is well-behaved, i.e., is not defined by an
  632.  * arbitrary PostScript procedure.
  633.  */
  634. bool
  635. zchar_show_width_only(const gs_text_enum_t * penum)
  636. {
  637.     if (!gs_text_is_width_only(penum))
  638.     return false;
  639.     switch (penum->orig_font->FontType) {
  640.     case ft_encrypted:
  641.     case ft_encrypted2:
  642.     case ft_CID_encrypted:
  643.     case ft_CID_TrueType:
  644.     case ft_CID_bitmap:
  645.     case ft_TrueType:
  646.     return true;
  647.     default:
  648.     return false;
  649.     }
  650. }
  651.  
  652. /* Shortcut the BuildChar or BuildGlyph procedure at the point */
  653. /* of the setcharwidth or the setcachedevice[2] if we are in */
  654. /* a stringwidth or cshow, or if we are only collecting the scalable */
  655. /* width for an xfont character. */
  656. private int
  657. op_show_return_width(i_ctx_t *i_ctx_p, uint npop, double *pwidth)
  658. {
  659.     uint index = op_show_find_index(i_ctx_p);
  660.     es_ptr ep = (es_ptr) ref_stack_index(&e_stack, index - (snumpush - 1));
  661.     int code = gs_text_setcharwidth(esenum(ep), pwidth);
  662.     uint ocount, dsaved, dcount;
  663.  
  664.     if (code < 0)
  665.     return code;
  666.     /* Restore the operand and dictionary stacks. */
  667.     ocount = ref_stack_count(&o_stack) - (uint) esodepth(ep).value.intval;
  668.     if (ocount < npop)
  669.     return_error(e_stackunderflow);
  670.     dsaved = (uint) esddepth(ep).value.intval;
  671.     dcount = ref_stack_count(&d_stack);
  672.     if (dcount < dsaved)
  673.     return_error(e_dictstackunderflow);
  674.     while (dcount > dsaved) {
  675.     code = zend(i_ctx_p);
  676.     if (code < 0)
  677.         return code;
  678.     dcount--;
  679.     }
  680.     ref_stack_pop(&o_stack, ocount);
  681.     /* We don't want to pop the mark or the continuation */
  682.     /* procedure (op_show_continue or cshow_continue). */
  683.     pop_estack(i_ctx_p, index - snumpush);
  684.     return o_pop_estack;
  685. }
  686.  
  687. /*
  688.  * Restore state after finishing, or unwinding from an error within, a show
  689.  * operation.  Note that we assume op == osp, and may reset osp.
  690.  */
  691. private int
  692. op_show_restore(i_ctx_t *i_ctx_p, bool for_error)
  693. {
  694.     register es_ptr ep = esp + snumpush;
  695.     gs_text_enum_t *penum = esenum(ep);
  696.     int saved_level = esgslevel(ep).value.intval;
  697.     int code = 0;
  698.  
  699.     if (for_error) {
  700.     uint saved_count = esodepth(ep).value.intval;
  701.     uint count = ref_stack_count(&o_stack);
  702.  
  703.     if (count > saved_count)    /* if <, we're in trouble */
  704.         ref_stack_pop(&o_stack, count - saved_count);
  705.     }
  706.     if (SHOW_IS_STRINGWIDTH(penum)) {    /* stringwidth does an extra gsave */
  707.     --saved_level;
  708.     }
  709.     if (penum->text.operation & TEXT_REPLACE_WIDTHS) {
  710.     gs_free_const_object(penum->memory, penum->text.y_widths, "y_widths");
  711.     if (penum->text.x_widths != penum->text.y_widths)
  712.         gs_free_const_object(penum->memory, penum->text.x_widths, "x_widths");
  713.     }
  714.     /*
  715.      * We might have been inside a cshow, in which case currentfont was
  716.      * reset temporarily, as though we were inside a BuildChar/ BuildGlyph
  717.      * procedure.  To handle this case, set currentfont back to its original
  718.      * state.  NOTE: this code previously used fstack[0] in the enumerator
  719.      * for the root font: we aren't sure that this change is correct.
  720.      */
  721.     gs_set_currentfont(igs, penum->orig_font);
  722.     while (igs->level > saved_level && code >= 0) {
  723.     if (igs->saved == 0 || igs->saved->saved == 0) {
  724.         /*
  725.          * Bad news: we got an error inside a save inside a BuildChar or
  726.          * BuildGlyph.  Don't attempt to recover.
  727.          */
  728.         code = gs_note_error(e_Fatal);
  729.     } else
  730.         code = gs_grestore(igs);
  731.     }
  732.     gs_text_release(penum, "op_show_restore");
  733.     return code;
  734. }
  735. /* Clean up after an error. */
  736. private int
  737. op_show_cleanup(i_ctx_t *i_ctx_p)
  738. {
  739.     return op_show_restore(i_ctx_p, true);
  740. }
  741. /* Clean up after termination of a show operation. */
  742. int
  743. op_show_free(i_ctx_t *i_ctx_p, int code)
  744. {
  745.     int rcode;
  746.  
  747.     esp -= snumpush;
  748.     rcode = op_show_restore(i_ctx_p, code < 0);
  749.     return (rcode < 0 ? rcode : code);
  750. }
  751.  
  752. /* Get a FontBBox parameter from a font dictionary. */
  753. int
  754. font_bbox_param(const ref * pfdict, double bbox[4])
  755. {
  756.     ref *pbbox;
  757.  
  758.     /*
  759.      * Pre-clear the bbox in case it's invalid.  The Red Books say that
  760.      * FontBBox is required, but the Adobe interpreters don't require
  761.      * it, and a few user-written fonts don't supply it, or supply one
  762.      * of the wrong size (!); also, PageMaker 5.0 (an Adobe product!)
  763.      * sometimes emits an absurd bbox for Type 1 fonts converted from
  764.      * TrueType.
  765.      */
  766.     bbox[0] = bbox[1] = bbox[2] = bbox[3] = 0.0;
  767.     if (dict_find_string(pfdict, "FontBBox", &pbbox) > 0) {
  768.     if (!r_is_array(pbbox))
  769.         return_error(e_typecheck);
  770.     if (r_size(pbbox) == 4) {
  771.         const ref_packed *pbe = pbbox->value.packed;
  772.         ref rbe[4];
  773.         int i;
  774.         int code;
  775.         float dx, dy, ratio;
  776.  
  777.         for (i = 0; i < 4; i++) {
  778.         packed_get(pbe, rbe + i);
  779.         pbe = packed_next(pbe);
  780.         }
  781.         if ((code = num_params(rbe + 3, 4, bbox)) < 0)
  782.         return code;
  783.         /* Require "reasonable" values.  Thanks to Ray */
  784.         /* Johnston for suggesting the following test. */
  785.         dx = bbox[2] - bbox[0];
  786.         dy = bbox[3] - bbox[1];
  787.         if (dx <= 0 || dy <= 0 ||
  788.         (ratio = dy / dx) < 0.125 || ratio > 8.0
  789.         )
  790.         bbox[0] = bbox[1] = bbox[2] = bbox[3] = 0.0;
  791.     }
  792.     }
  793.     return 0;
  794. }
  795.